"use strict";
cc._RF.push(module, 'a67e5ZuosVEbY5m60vgLu/p', 'bet_115_directSelect');
// scripts/bet/bet_115_directSelect.js

"use strict";

// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html
/**
 * 投注面板
 * 11选5直选投注面板
 */
cc.Class({
    extends: cc.Component,

    properties: {
        tgAnySelList: [cc.Toggle], //选择玩法按钮列表
        tgRedBall: cc.Prefab, //球的预制
        betContent: [cc.Node], //球放置目录
        labWeiDec: [cc.Label], //位数
        labTopTitle: cc.Label, //标头
        labDec: cc.RichText, //说明
        tgSelect: cc.Toggle, //机选、清空切换按钮
        edMutipleNum: cc.EditBox, //投的倍数
        labShowAmount: cc.Label, //显示共注、倍、元、
        manageCenter: cc.Node,
        scrollview: cc.ScrollView,
        BASEMONEY: 2, //基础投资
        isMiss: false, //是否开启遗漏
        betToggleList: [], //球指针列表
        selRedBall: [], //选择球数字列表
        isLoadRes: [], //是否加载   
        curMoney: 0,
        curBetNum: 0,
        curPage: 1
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start: function start() {
        this.initConfig();
        this.initPanel();
    },


    initConfig: function initConfig() {
        this.rules = [DEFINE.LOTTERYRULE115.ZHI_ONE, DEFINE.LOTTERYRULE115.ZHI_TWO, DEFINE.LOTTERYRULE115.ZHI_THREE];

        this.nums = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11"];
        this.decContents = ["<color=#ffffff>至少选1个号码，猜对第1个开奖号即中</c><color=#fcd346>13元宝</color>", "<color=#ffffff>每位至少选1个号码，按位猜对前2个开奖号即中</c><color=#fcd346>130元宝</color>", "<color=#ffffff>每位至少选1个号码，按位猜对前3个开奖号即中</c><color=#fcd346>1170元宝</color>"];
        //this.names = ["前一直选","前二直选","前三直选"];

        for (var i = 0; i < 3; i++) {
            var arry = [];
            this.isLoadRes.push(false);
            this.selRedBall.push(arry);
        }
        this._betManage = this.manageCenter.getComponent("bet_115_pop");
    },

    initPanel: function initPanel() {
        this.curPage = 1;
        this.refreshPage(this.curPage);
        for (var i = 0; i < this.tgAnySelList.length; i++) {
            var checkEventHandler = new cc.Component.EventHandler();
            checkEventHandler.target = this.node;
            checkEventHandler.component = "bet_115_directSelect";
            checkEventHandler.handler = "onSelPageCallBack";
            checkEventHandler.customEventData = {
                //name:this.names[i],
                pageID: i + 1,
                decContent: this.decContents[i]
            };
            this.tgAnySelList[i].getComponent(cc.Toggle).checkEvents.push(checkEventHandler);
        }
        //初始选择第一个小界面
        var temp = this.tgAnySelList[0].getComponent(cc.Toggle).checkEvents[0].customEventData;
        this.onSelPageCallBack(this.tgAnySelList[0], temp);
        this.isLoadRes[0] = true;
    },

    onSelPageCallBack: function onSelPageCallBack(toggle, customEventData) {
        if (toggle.getComponent(cc.Toggle).isChecked) {
            this.scrollview.scrollToTop(0.1);
            //this.labTopTitle.string = customEventData.name;
            this.labDec.string = customEventData.decContent;
            this.curPage = customEventData.pageID;
            this.refreshPage(this.curPage);
            this.clearCurSel();
            // if(this.isMiss)  //遗漏
            // {
            //     this._betManage.showMissArry(this.betToggleList,this.lotteryID,this.lotteryID+this.rules[this.curPage-1].toString());
            // }
        }
    },

    //清空当前选择
    clearCurSel: function clearCurSel() {
        for (var i = 0; i < this.selRedBall.length; i++) {
            this.selRedBall[i].length = 0;
        }

        for (var i = 0; i < this.betToggleList.length; i++) {
            for (var j = 0; j < this.betToggleList[i].length; j++) {
                this.betToggleList[i][j].getComponent(cc.Toggle).isChecked = false;
            }
        }

        this.tgSelect.getComponent(cc.Toggle).isChecked = false;
        this._betManage.setMutipleAmount(1);
        this.checkBet();
    },

    refreshPage: function refreshPage(index) {
        if (index > 1) {
            this.labWeiDec[0].string = "万位";
        } else {
            this.labWeiDec[0].string = "选号";
        }
        for (var i = 0; i < 3; i++) {
            if (index >= i + 1) {
                if (this.isLoadRes[i]) {
                    this.betContent[i].active = true;
                    this.labWeiDec[i].node.active = true;
                } else {
                    var arry = [];
                    for (var j = 0; j < this.nums.length; j++) {
                        var ball = cc.instantiate(this.tgRedBall);
                        ball.getComponent('bet_sel115_item').init({
                            num: this.nums[j],
                            miss: ""
                        });
                        var checkEventHandler = new cc.Component.EventHandler();
                        checkEventHandler.target = this.node;
                        checkEventHandler.component = "bet_115_directSelect";
                        checkEventHandler.handler = "onClickCallback";
                        checkEventHandler.customEventData = { num: j + 1, type: i };
                        ball.getComponent(cc.Toggle).checkEvents.push(checkEventHandler);
                        this.betContent[i].addChild(ball);
                        arry.push(ball);
                    }
                    this.betToggleList.push(arry);
                    this.isLoadRes[i] = true;
                    this.betContent[i].active = true;
                    this.labWeiDec[i].node.active = true;
                }
            } else {
                this.labWeiDec[i].node.active = false;
                this.betContent[i].active = false;
            }
        }
    },

    onClickCallback: function onClickCallback(toggle, customEventData) {
        var num = customEventData.num;
        var type = customEventData.type;
        if (toggle.getComponent(cc.Toggle).isChecked) {
            for (var i = 0; i < this.betToggleList.length; i++) {
                if (i == type) continue;

                if (this.betToggleList[i][num - 1].getComponent(cc.Toggle).isChecked) {
                    this.betToggleList[i][num - 1].getComponent(cc.Toggle).isChecked = false;
                    Utils.removeByValue(this.selRedBall[i], num);
                    break;
                }
            }
            this.selRedBall[type].push(num);
        } else {
            Utils.removeByValue(this.selRedBall[type], num);
        }
        this.updateTgSelect();
        this.checkBet();
    },

    updateTgSelect: function updateTgSelect() {
        for (var i = 0; i < this.selRedBall.length; i++) {
            if (this.selRedBall[i].length > 0) {
                this.tgSelect.getComponent(cc.Toggle).isChecked = true;
                return;
            }
        }
        this.tgSelect.getComponent(cc.Toggle).isChecked = false;
    },

    //机选
    randomSelectCallBack: function randomSelectCallBack() {
        this.clearCurSel();
        var tempnum = [];
        for (var i = 0; i < this.curPage; i++) {
            var randomArray = Utils.getRandomArrayWithArray(this.betToggleList[i], 1);
            var temp = randomArray[0].getComponent(cc.Toggle).checkEvents[0].customEventData;

            while (1) {
                if (Utils.findValueByArry(tempnum, temp.num)) {
                    randomArray = Utils.getRandomArrayWithArray(this.betToggleList[i], 1);
                    temp = randomArray[0].getComponent(cc.Toggle).checkEvents[0].customEventData;
                } else {
                    break;
                }
            }

            tempnum.push(temp.num);
            randomArray[0].getComponent(cc.Toggle).isChecked = true;
            this.onClickCallback(randomArray[0], temp);
        }
    },

    //选择清空切换
    onSelectCallBack: function onSelectCallBack(toggle) {
        if (this.isNorm == 0) {
            toggle.getComponent(cc.Toggle).isChecked = true;
            this.clearCurSel();
        } else {
            if (toggle.getComponent(cc.Toggle).isChecked) {
                this.randomSelectCallBack();
            } else {
                this.clearCurSel();
            }
        }
    },

    //计算投注规则
    checkBet: function checkBet() {
        var bet = 1;
        for (var i = 0; i < this.curPage; i++) {
            if (this.selRedBall[i].length == 0) {
                bet = 0;
                break;
            } else {
                bet = this.selRedBall[i].length * bet;
            }
        }
        var muiple = this._betManage.getMutipleAmount();
        this.setShowAmount(muiple, bet);
    },

    //显示投注信息
    setShowAmount: function setShowAmount(mut, bet) {
        var issue = 1; //this.getIssueNum();
        var money = mut * bet * this.BASEMONEY * issue;
        this._betManage.setMoney(money);
        this._betManage.setBetNum(bet);
        this.labShowAmount.string = "共" + bet + "注" + mut + "倍" + issue + "期" + money + "元宝";
    },

    //投注信息组合
    dataToJson: function dataToJson() {
        var obj = new Object();
        obj.PlayCode = parseInt(this._betManage._lotteryCode + this.rules[this.curPage - 1].toString());
        var num = "";
        var nums = "";
        for (var i = 0; i < this.curPage; i++) {
            Utils.sortNum(this.selRedBall[i]);
            num = "";
            for (var j = 0; j < this.selRedBall[i].length; j++) {
                if (num != "") {
                    num += ",";
                }
                var oneNum = this.selRedBall[i][j] > 9 ? this.selRedBall[i][j].toString() : "0" + this.selRedBall[i][j].toString();
                num += oneNum;
            }
            if (nums == "") {
                nums = num;
            } else {
                nums = nums + "|" + num;
            }
        }

        var arry = [];
        var numArrays = {
            "Multiple": this._betManage.getMutipleAmount(),
            "Bet": this._betManage.getBetNum(),
            "isNorm": 1,
            "Number": nums
        };
        arry.push(numArrays);
        obj.Data = arry;
        var json = JSON.stringify(obj);
        cc.log("提交订单：" + json);
        return json;
    },

    showMissArry: function showMissArry(toggleArryList, lotteryid, playCode) {
        var recv = function (ret) {
            ComponentsUtils.unblock();
            var temparr = [];
            if (ret.Code === 0) {
                temparr = ret.Data;

                var obj = eval('(' + temparr + ')');
                this.setMissArry(true, toggleArryList, obj["o"]);
            } else {
                ComponentsUtils.showTips(ret.Msg);
                this.setMissArry(true, toggleArryList, temparr);
            }
        }.bind(this);
        var data = {
            LotteryCode: lotteryid,
            IsuseName: "1",
            PlayCode: playCode,
            TopCount: 100
        };
        CL.HTTP.sendRequest(DEFINE.HTTP_MESSAGE.GETLOTTERYMISS, data, recv.bind(this), "POST");
        ComponentsUtils.block();
    },

    //是否显示-遗漏数组-显示控件列表
    setMissArry: function setMissArry(isMiss, toggleList, missArry) {
        if (isMiss) {
            var arrtemp = [];
            if (missArry.length > 5) {
                arrtemp.push(missArry);
            } else arrtemp = missArry;
            if (arrtemp.length > 0) {
                for (var i = 0; i < arrtemp.length; i++) {
                    for (var j = 0; j < toggleList[i].length; j++) {
                        toggleList[i][j].getComponent('bet_sel115_item').setMissNum(arrtemp[i][j]);
                    }
                }
            } else {
                for (var i = 0; i < toggleList.length; i++) {
                    for (var j = 0; j < toggleList[j].length; j++) {
                        toggleList[i][j].getComponent('bet_sel115_item').setMissNum("--");
                    }
                }
            }
        } else {
            for (var i = 0; i < toggleList.length; i++) {
                for (var j = 0; j < toggleList[i].length; j++) {
                    toggleList[i][j].getComponent('bet_sel115_item').setMissNum("");
                }
            }
        }
    }

    // update (dt) {},
});

cc._RF.pop();